//=============================================================================
/**
 *
 *	@file		large_yesno.c
 *	@brief		BMP選択ウィンドウモジュール
 *	@author		hosaka genya
 *	@data		2009.03.28
 *	@note		BANK割り当てが32kあれば使用できます。
 *
 */
//=============================================================================
#include "common.h"
#include "system/arc_tool.h"
#include "system/arc_util.h"
#include "system/window.h"

#include "system/bmp_menu.h"
#include "system/msgdata.h"
#include "system/fontproc.h"
#include "system/font_arc.h"
#include "system/msgdata_util.h"
#include "system/wordset.h"

#include "system/clact_util.h"
#include "system/clact_tool.h"

#include "gflib/touchpanel.h"

#include "msgdata/msg.naix"
#include "application/subselect.naix"

#include "system/snd_tool.h"

#include "large_yesno.h"

//=============================================================================
/**
 *								定数定義
 */
//=============================================================================
// はい・いいえ用BMPWIN定義
#define YESNO_BMP_X		( 4 )
#define YESNO_BMP_Y1	(  8 )
#define YESNO_BMP_Y2	( 14 )
#define YESNO_BMP_W		(  24 )
#define YESNO_BMP_H		(  2 )
#define YESNO_BMP_OFFSET	( 80 )

static const s16 yesno_cursor_pos_tbl[][2]={
	{ 128, 72 },
	{ 128, 120 },
};

static const RECT_HIT_TBL yesno_touch_tbl[] = {
	{ 50,  92, 3, 251, },
	{ 99, 140, 3, 251, },
	{TP_HIT_END,0,0,0},		// 終了データ
};

//=============================================================================
/**
 *								構造体定義
 */
//=============================================================================

//--------------------------------------------------------------
///	メインワーク
//==============================================================
typedef struct LARGE_YESNO_WORK_tag {
	// [IN]
	HEAPID HeapID;
	CATS_SYS_PTR csp;
	CATS_RES_PTR crp;
	GF_BGL_INI* bgl;

	// [PRIVATE]
	CLACT_WORK_PTR	actCursor;
	WORDSET*		wordset;

	u8				text_palofs;
	u8				frame_screen;
	u8				frame_text;
	u8				select_seq : 4;
	u8				select_ret : 4;
	GF_BGL_BMPWIN	YesNoBmpWin[2];
} LARGE_YESNO_WORK;


//=============================================================================
/**
 *							プロトタイプ宣言
 */
//=============================================================================

static void Bmpwin_Add( LARGE_YESNO_PTR ptr );
static void Bmpwin_Del( LARGE_YESNO_PTR ptr );
static void Bmpwin_Msgdraw( LARGE_YESNO_PTR ptr, GF_BGL_BMPWIN *win, int msgno, int msgdat );
static void Obj_SetCursor( LARGE_YESNO_PTR ptr );
static BOOL Seq_Input(LARGE_YESNO_PTR ptr);
static BOOL Seq_AnimeWait( LARGE_YESNO_PTR ptr );

//=============================================================================
/**
 *								外部公開関数
 */
//=============================================================================

//-----------------------------------------------------------------------------
/**
 *	@brief	モジュール生成
 *
 *	@param	GF_BGL_INI* bgl
 *	@param	act
 *	@param	HeapID
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
LARGE_YESNO_PTR LargeYesno_Create( GF_BGL_INI* bgl, CLACT_WORK_PTR act, int frame_screen, int frame_text, int text_palofs, HEAPID HeapID )
{
	LARGE_YESNO_PTR ptr;

	GF_ASSERT( bgl );

	ptr = sys_AllocMemory( HeapID, sizeof(LARGE_YESNO_WORK) );
	MI_CpuClear8( ptr, sizeof(LARGE_YESNO_WORK) );

	ptr->HeapID = HeapID;
	ptr->bgl = bgl;
	ptr->actCursor = act;
	ptr->frame_screen = frame_screen;
	ptr->frame_text = frame_text;
	ptr->text_palofs = text_palofs;
	ptr->wordset = WORDSET_Create( HeapID );

	// オートアニメON
	CLACT_SetAnmFlag( act, 1 );

	Bmpwin_Add( ptr );

	return ptr;
}

//-----------------------------------------------------------------------------
/**
 *	@brief
 *
 *	@param	LARGE_YESNO_PTR ptr
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
void LargeYesno_Delete( LARGE_YESNO_PTR ptr )
{
	GF_ASSERT( ptr );

	Bmpwin_Del( ptr );

	WORDSET_Delete( ptr->wordset );

	sys_FreeMemoryEz( ptr );
}

//-----------------------------------------------------------------------------
/**
 *	@brief
 *
 *	@param	LARGE_YESNO_PTR ptr
 *	@param	pal_line
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
void LargeYesno_LoadScreen( LARGE_YESNO_PTR ptr, int pal_line )
{
	ARCHANDLE* arcSubSelect;

	int arc_index = ARC_SUBSELECT_GRA;
	int pal_index = NARC_subselect_bmpmenu_nclr;
	int chr_index = NARC_subselect_bmpmenu_bg_ncgr;
	int scr_index = NARC_subselect_bmpmenu_yn_nscr;
	int heap_id	  = ptr->HeapID;
	int frame = ptr->frame_screen;

	arcSubSelect = ArchiveDataHandleOpen( ARC_SUBSELECT_GRA, heap_id );

	// 選択肢
	ArcUtil_HDL_PalSet( arcSubSelect, pal_index, PALTYPE_SUB_BG, 0x20 * pal_line, 0x20, heap_id );
	ArcUtil_HDL_BgCharSet( arcSubSelect, chr_index, ptr->bgl, frame, 0, 0, 0, heap_id );
	ArcUtil_HDL_ScrnSet( arcSubSelect, scr_index, ptr->bgl, frame, 0, 0, 0, heap_id );

	// スクリーンのパレットチェンジ
	GF_BGL_ScrPalChange( ptr->bgl, frame, 0, 0, 32, 24, pal_line );
	GF_BGL_LoadScreenReq( ptr->bgl, frame );

	// 非表示にしておく
	GF_BGL_VisibleSet( frame, VISIBLE_OFF );

	ArchiveDataHandleClose( arcSubSelect );
}

//-----------------------------------------------------------------------------
/**
 *	@brief	選択肢を初期化します。
 *
 *	@param	ptr
 *	@param	msg1:YESに
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
void LargeYesno_Req(LARGE_YESNO_PTR ptr, int msgdat, int msg1, int msg2 )
{
	// 描画
	Bmpwin_Msgdraw( ptr, &ptr->YesNoBmpWin[0], msg1, msgdat );
	Bmpwin_Msgdraw( ptr, &ptr->YesNoBmpWin[1], msg2, msgdat );

	ptr->select_seq = 0;
	ptr->select_ret = LARGE_YESNO_YES;
	Obj_SetCursor( ptr );

	// 選択肢BG表示
	GF_BGL_VisibleSet( ptr->frame_screen, VISIBLE_ON );
	GF_BGL_VisibleSet( ptr->frame_text, VISIBLE_ON );

	CLACT_SetDrawFlag( ptr->actCursor, 1 );
}


//-----------------------------------------------------------------------------
/**
 *	@brief	選択肢 入力待ち
 *
 *	@param	ptr
 *
 *	@retval	LARGE_YESNO_RES:入力結果を返す
 */
//-----------------------------------------------------------------------------
LARGE_YESNO_RES LargeYesno_ChcekInputProc(LARGE_YESNO_PTR ptr)
{
	switch(ptr->select_seq)
	{
		case 0:
			if( Seq_Input(ptr) ) {
				ptr->select_seq++;
			}
			break;
		case 1:
			// アニメ終了待ち
			if( Seq_AnimeWait(ptr) ){
				GF_ASSERT( ptr->select_ret != LARGE_YESNO_NONE );
				GF_BGL_VisibleSet( ptr->frame_screen, VISIBLE_OFF );
				GF_BGL_VisibleSet( ptr->frame_text, VISIBLE_OFF );
				CLACT_SetDrawFlag( ptr->actCursor, 0 );
				return ptr->select_ret;
			}
			break;

		default : GF_ASSERT(0);
	}

	return LARGE_YESNO_NONE;
}




//=============================================================================
/**
 *								static関数
 */
//=============================================================================

//-----------------------------------------------------------------------------
/**
 *	@brief
 *
 *	@param	LARGE_YESNO_PTR ptr
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
static void Bmpwin_Add( LARGE_YESNO_PTR ptr )
{
	GF_ASSERT(ptr);

	GF_BGL_BmpWinAdd( ptr->bgl, &ptr->YesNoBmpWin[0], ptr->frame_text,
					  YESNO_BMP_X, YESNO_BMP_Y1, YESNO_BMP_W, YESNO_BMP_H,
					  ptr->text_palofs, YESNO_BMP_OFFSET	);

	GF_BGL_BmpWinAdd( ptr->bgl, &ptr->YesNoBmpWin[1], ptr->frame_text,
					  YESNO_BMP_X, YESNO_BMP_Y2, YESNO_BMP_W, YESNO_BMP_H,
					  ptr->text_palofs, YESNO_BMP_OFFSET+YESNO_BMP_W*YESNO_BMP_H );
}

//-----------------------------------------------------------------------------
/**
 *	@brief
 *
 *	@param	LARGE_YESNO_PTR ptr
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
static void Bmpwin_Del( LARGE_YESNO_PTR ptr )
{
	int i;

	GF_ASSERT( ptr );

	for( i=0; i<2; i++ )
	{
		GF_BGL_BmpWinDel( &ptr->YesNoBmpWin[i] );
	}
}

//------------------------------------------------------------------
/**
 * @brief   BMPWINにメッセージ描画
 *
 * @param   win
 * @param   msgno
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void Bmpwin_Msgdraw( LARGE_YESNO_PTR ptr, GF_BGL_BMPWIN *win, int msgno, int msgdat )
{
	STRBUF *strbuf;
	MSGDATA_MANAGER* msg_man;

	msg_man = MSGMAN_Create( MSGMAN_TYPE_NORMAL, ARC_MSG, msgdat, ptr->HeapID );
	strbuf = MSGDAT_UTIL_AllocExpandString( ptr->wordset, msg_man, msgno, ptr->HeapID );

	GF_BGL_BmpWinDataFill( win,  0x0000 );
	GF_STR_PrintColor( win, FONT_TOUCH, strbuf, 0, 0, MSG_ALLPUT, GF_PRINTCOLOR_MAKE( 15,1,0 ),NULL);
	GF_BGL_BmpWinOn( win );

	MSGMAN_Delete( msg_man );
	STRBUF_Delete( strbuf );
}

//-----------------------------------------------------------------------------
/**
 *	@brief	カーソルACT 座標セット
 *
 *	@param	LARGE_YESNO_PTR ptr
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
static void Obj_SetCursor( LARGE_YESNO_PTR ptr )
{
	int idx;

	GF_ASSERT( ptr->actCursor );

	switch( ptr->select_ret )
	{
		case LARGE_YESNO_YES : idx = 0; break;
		case LARGE_YESNO_NO  : idx = 1; break;
		default : GF_ASSERT(0);
	}

	CATS_ObjectPosSet( ptr->actCursor, yesno_cursor_pos_tbl[idx][0], yesno_cursor_pos_tbl[idx][1] );
}

//-----------------------------------------------------------------------------
/**
 *	@brief	シーケンス：入力
 *
 *	@param	ptr
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
static BOOL Seq_Input(LARGE_YESNO_PTR ptr)
{
	BOOL b_ret = FALSE;
	u32 tp_ret = GF_TP_RectHitTrg( yesno_touch_tbl );

	if( tp_ret != RECT_HIT_NONE ) {
		Snd_SePlay( SE_DECIDE );
		switch( tp_ret ) {
			case 0 : ptr->select_ret = LARGE_YESNO_YES; b_ret = TRUE; break;
			case 1 : ptr->select_ret = LARGE_YESNO_NO; b_ret = TRUE; break;
			default : GF_ASSERT(0);
		}
	}
	// KEY操作
	else
	{
		if(sys.trg&PAD_KEY_UP){
			if( ptr->select_ret != LARGE_YESNO_YES ) {
				ptr->select_ret = LARGE_YESNO_YES;
				Obj_SetCursor( ptr );
				Snd_SePlay( SE_DECIDE );
			}
		}else if(sys.trg&PAD_KEY_DOWN){
			if( ptr->select_ret != LARGE_YESNO_NO ) {
				ptr->select_ret = LARGE_YESNO_NO;
				Obj_SetCursor( ptr );
				Snd_SePlay( SE_DECIDE );
			}
		}else if(sys.trg&PAD_BUTTON_DECIDE){
			// 不具合対策
			if( ptr->select_ret == LARGE_YESNO_NONE ) {
				GF_ASSERT(0);
				ptr->select_ret = LARGE_YESNO_NO;
			}
			b_ret = TRUE;
		}else if(sys.trg&PAD_BUTTON_CANCEL){
			ptr->select_ret = LARGE_YESNO_NO;
			b_ret = TRUE;
		}
	}

	if( b_ret ) {
		Obj_SetCursor( ptr );
		CLACT_AnmChg( ptr->actCursor, 3 );
		Snd_SePlay( SE_DECIDE );
	}

	return b_ret;
}

//-----------------------------------------------------------------------------
/**
 *	@brief	シーケンス：アニメ終了待ち
 *
 *	@param	LARGE_YESNO_PTR ptr
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
static BOOL Seq_AnimeWait( LARGE_YESNO_PTR ptr )
{
	GF_ASSERT( CLACT_AnmGet( ptr->actCursor) == 3 );

	if( CLACT_AnmActiveCheck(ptr->actCursor) == FALSE ) {
		return TRUE;
	}

	return FALSE;
}

